package com.xunua.NaoZiKongBai.SplashAndSchemeActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.gyf.immersionbar.ImmersionBar;
import com.xunua.NaoZiKongBai.Bean.Bmob.MyUser;
import com.xunua.NaoZiKongBai.LoginActivity.LoginActivity;
import com.xunua.NaoZiKongBai.MainActivity.MainActivity;
import com.xunua.NaoZiKongBai.R;
import com.xunua.NaoZiKongBai.SplashAndSchemeActivity.openingstartanimation.NormalDrawStrategy;
import com.xunua.NaoZiKongBai.SplashAndSchemeActivity.openingstartanimation.OpeningStartAnimation;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;

/**
 * 闪屏页   三秒钟广告图
 */
public class SplashActivity extends AppCompatActivity {

    @BindView(R.id.splash_statusBar)
    View splashStatusBar;
    @BindView(R.id.splash_rel)
    RelativeLayout splashRel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        splashAnimation();
        ImmersionBarInit();
    }


    //开屏动画 splash
    private void splashAnimation() {
        OpeningStartAnimation myOpeningStartAnimation = new OpeningStartAnimation.Builder(this)
//                .setColorOfAppStatement(getResources().getColor(R.color.colorWhite)) // 设置一句话描述的颜色  //隐藏
                .setAppStatement(getResources().getString(R.string.app_name)) //设置一句话描述
//                .setAnimationInterval(1000) // 设置动画时间间隔
//                .setAnimationFinishTime(99999999) // 设置动画的消失时长
                .setDrawStategy(new NormalDrawStrategy()) //设置动画效果
                .create();
        myOpeningStartAnimation.setCompleatListenser(new OpeningStartAnimation.OnCompleatListener() {
            @Override
            public void isOk() {
                islogin();
            }
        });
        myOpeningStartAnimation.show(this);
    }

    /**
     * 沉浸式状态栏
     */
    private void ImmersionBarInit() {
        //状态栏标准模式
        ImmersionBar.with(this)
                .statusBarView(splashStatusBar)
                .keyboardEnable(true)//解决软键盘与底部输入框冲突问题
                .statusBarDarkFont(true)//状态栏字体颜色
                .navigationBarColor(R.color.navigationBarcolor)//导航栏颜色，不写默认黑色
                .navigationBarDarkIcon(true)//导航栏图标是深色，不写默认为亮色
                .init();//沉浸状态栏
    }

    /**
     * 用于判断登录状态。跳转登录页面或者直接进入主页。
     */
    private void islogin() {
        MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
        if (myUser != null) {
            // 允许用户使用应用
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            //缓存用户对象为空时， 可打开用户登录界面…
            Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            //消费返回键点击事件。闪屏页禁止用户点击返回键.
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 暂时弃用
     *
     *
     * //        添加动画 AnimationUtils拿到动画对象
     * //        Animation animation = AnimationUtils.loadAnimation(SplashActivity.this, R.anim.splash);
     * //        animation.setFillAfter(false);
     * //        splashRel.startAnimation(animation);
     *
     *
     * 展示一秒钟的展示图。
     *     private void time1s() {
     *         Observable.interval(3, TimeUnit.SECONDS)
     *                 .takeWhile(new Predicate<Long>() {
     *                     @Override
     *                     public boolean test(Long aLong) throws Exception {
     *                         if (aLong == 0) {
     *                             return true;
     *                         }else{
     *                             return false;
     *                         }
     *                     }
     *                 })
     *                 .subscribeOn(Schedulers.newThread())
     *                 .observeOn(AndroidSchedulers.mainThread())
     *                 .subscribe(new Consumer<Long>() {
     *                     @Override
     *                     public void accept(Long aLong) throws Exception {
     *                         ImmersionBarInit();
     *                         islogin();
     *                     }
     *                 });
     *     }
     *
     *
     */

}
